package com.star.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

//http://www.ln.chinanews.com/html/2016-04-02/1318270.html
//http://www.ln.chinanews.com/html/2016-04-02/1318269.html
public class HtmlContentGet {
	public String getContent(String url) {
		String host = url.split("//")[1].split("/")[0];
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		/// 设置请求报头，模拟Chrome浏览器
		httpGet.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
		httpGet.addHeader("Accept-Encoding", "gzip,deflate,sdch");
		httpGet.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
		httpGet.addHeader("Content-Type", "text/html; charset=UTF-8");
		httpGet.addHeader("Host", host);
		httpGet.addHeader("User-Agent",
				"Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36");
		CloseableHttpResponse response;
		String content = "";
		try {
			response = httpClient.execute(httpGet);
			Header[] headers = response.getAllHeaders();
			/// 从HTTP响应中取出页面内容
			HttpEntity repEntity = response.getEntity();
			InputStream inputStream = repEntity.getContent();
			BufferedReader read = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
			String charset = "UTF-8";
			while (true) {
				String tempContent = read.readLine();
				if (tempContent == null) {
					break;
				}
				if (tempContent.contains("charset")) {
					if (tempContent.toLowerCase().contains("gbk")) {
						charset = "gbk";
					}
					if (tempContent.toLowerCase().contains("gb2312")) {
						charset = "gb2312";
					}
					break;
				}
			}
			content = EntityUtils.toString(repEntity, charset);
			// /// 打印出页面的内容：
			response.close();
			httpClient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Document document = Jsoup.parse(content);
		Element element = document.body();
//		if (element == null) {
//			element = document.select("div.article").first();
//		}
//		if (element == null) {
//			element = document.select("div.show_body").first();
//		}
//		if (element == null) {
//			element = document.select("div.show-content").first();
//		}
//		if(element == null){
//			element = document.select("div.contents").first();
//		}
		return element == null ? "抓取正文部分失败" : element.text();
	}
}
